package com.example.shopping2.a_a_mvp.model.models

import BaseModel
import com.example.perplonews.a_c_network.Api
import com.example.perplonews.a_c_network.ChangFunction
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_c_fragment.car.entity.CarList_Entity
import com.example.shopping2.a_c_fragment.car.entity.OrderGoodsEntity
import com.example.shopping2.a_c_fragment.car.entity.ShipAddressEntity
import com.example.shopping2.a_c_fragment.car.entity.SubCarEntity
import com.example.shopping2.a_c_fragment.classa.entity.ClassRight_Entity
import com.example.shopping2.a_c_fragment.classa.entity.Classleft_Entity
import com.example.shopping2.a_c_fragment.home.entity.HomeGoodsEntity
import com.example.shopping2.a_c_fragment.main.entity.greendao.User
import com.example.shopping2.a_c_fragment.main.entity.greendao.UserValue
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.HttpFactory
import com.example.shopping2.a_c_network.HttpType
import io.reactivex.Observable
import okhttp3.RequestBody

class HomeModel : HomeContract.HomeModel, BaseModel() {
    override fun requestAll(list: ArrayList<HashMap<String, Any>>): Observable<BaseEntity> {
        var obs = ArrayList<Observable<BaseEntity>>()
        var i = 0
        while (i < list.size) {
            var hashMap = list[i]
            var code = hashMap.get("code")
            hashMap.remove("code")
            when (code) {
                HttpCode.HOMEDATA -> {
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestHomeGoods(createBody(hashMap)!!)
                        .map(ChangFunction<HomeGoodsEntity>())
                    obs.add(map)

                }
                HttpCode.CLASSLEFT -> {
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestClassValue(createBody(hashMap)!!)
                        .map(ChangFunction<Classleft_Entity>())
                    obs.add(map)
                }
                HttpCode.CLASSRIGHT -> {
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestClassValue2(createBody(hashMap)!!)
                        .map(ChangFunction<ClassRight_Entity>())
                    obs.add(map)
                }
                HttpCode.LOGIN ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestLogin(createBody(hashMap)!!)
                        .map(ChangFunction<UserValue>())
                    obs.add(map)
                }
                HttpCode.ADDCAR ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestAddCar(createBody(hashMap)!!)
                        .map(ChangFunction<BaseEntity>())
                    obs.add(map)
                }
                HttpCode.CARLIST ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestCarList(createBody(hashMap))
                        .map(ChangFunction<CarList_Entity>())
                    obs.add(map)
                }
                HttpCode.COMMITCAR ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestSubCar(createBody(hashMap))
                        .map(ChangFunction<SubCarEntity>())
                    obs.add(map)
                }
                HttpCode.ORDER ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestGetOrder(createBody(hashMap))
                        .map(ChangFunction<OrderGoodsEntity>())
                    obs.add(map)
                }
                HttpCode.ADDRESSLIST ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestAddressList(createBody(hashMap))
                        .map(ChangFunction<ShipAddressEntity>())
                    obs.add(map)
                }
                HttpCode.ADDADDRESS ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestAddAddress(createBody(hashMap))
                        .map(ChangFunction<BaseEntity>())
                    obs.add(map)
                }
                HttpCode.EDIADDRESS ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestUpdateAddress(createBody(hashMap))
                        .map(ChangFunction<BaseEntity>())
                    obs.add(map)
                }
                HttpCode.DELETEADD ->{
                    var map = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestDelAddress(createBody(hashMap))
                        .map(ChangFunction<BaseEntity>())
                    obs.add(map)
                }




            }


            i++
        }


        return Observable.merge(obs)
    }


}